import { invoke } from '@tauri-apps/api/core'
import { platform } from '@tauri-apps/plugin-os'
import { Command, open } from '@tauri-apps/plugin-shell'
import { ElMessage } from 'element-plus'

export function moveCenter(el: HTMLDivElement | null) {
    setTimeout(() => {
        const target = el?.querySelector(".active") as HTMLDivElement
        const box: HTMLDivElement | undefined | null = el?.querySelector('.box')
        if (!el || !box || !target) return
        if (el.clientWidth >= box.clientWidth) return
        const max = el.clientWidth - box.clientWidth
        let left = -target.offsetLeft + (el.clientWidth - target.clientWidth) / 2
        left = Math.max(Math.min(left, 0), max)
        box.style.left = left + "px"
    }, 100)
}
export function moveBy(el: HTMLDivElement | null, type: 'l' | 'r' = 'l') {
    const box: HTMLDivElement | undefined | null = el?.querySelector('.box')
    if (!el || !box) return
    if (el.clientWidth >= box.clientWidth) return
    let left = parseInt(box.style.left) || 0
    const max = el.clientWidth - box.clientWidth
    if (type === 'l') {
        left += 100
    } else {
        left -= 100
    }
    left = Math.max(Math.min(left, 0), max)
    box.style.left = left + "px"
}

export async function openInFinder(path: string) {
    if (!path) return
    switch (getOs()) {
        case "windows":
            await open(path)
            break
        case "macos":
            await open(path)
            break
        default:
            ElMessage.warning("暂不支持该操作系统")
            break
    }
}
export async function openInVsCode(path: string) {
    if (!path) return
    switch (getOs()) {
        case "windows":
            exec("code .", path)
            break
        case "macos":
            await invoke('open_with_vscode', { path })
            break
        default:
            ElMessage.warning("暂不支持该操作系统")
            break
    }
}

export function getOs() {
    return platform()
}

export async function exec(command: string, path: string, result?: { type: string, value: string }[], cb?: (type?: string) => void) {
    let com
    switch (getOs()) {
        case "windows":
            com = Command.create("exec-cmd", ["/C", command], { cwd: path })
            break
        case "macos":
            com = Command.create("exec-sh", ["-c", command], { cwd: path })
            break
        default:
            ElMessage.warning("暂不支持该操作系统")
            return
    }
    com.stdout.on("data", line => {
        if (result) {
            if (isSame(result[result.length - 1].value, line)) {
                result[result.length - 1].value = line
            } else {
                result.push({ type: "info", value: line })
            }
        }
    })
    com.stderr.on("data", line => {
        if (result) {
            result.push({ type: "error", value: line })
        }
    })
    com.on("error", () => {
        cb && cb('error')
    })
    com.on("close", () => {
        cb && cb()
    })
    const res = await com.spawn()
    return res
}

function isSame(oldVal: string, newVal: string): boolean {
    return oldVal.replace(/\d/g, '') === newVal.replace(/\d/g, '')
}